package com.iona.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iona.domain.BaseEntity;
import com.iona.domain.Templates;
import com.iona.json.JSONException;
import com.iona.utils.CreatorUtil;
import com.iona.utils.Storage;

/**
 * Servlet implementation class AddTemplatesServlet
 */
@WebServlet("/addtemplates")
public class AddTemplatesServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		super.init();
		setTransactional(true);
	}

	@Override
	public void doExecute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			JSONException {
		String qualification = request.getParameter("qualification");
		String formOfStudy = request.getParameter("formOfStudy");
		String author = request.getParameter("author");
		String statement = request.getParameter("statement");
		String dateOfStatement = request.getParameter("dateOfStatement");
		String numberOfProtocol = request.getParameter("numberOfProtocol");
		String toKnow = request.getParameter("toKnow");
		String beAbleTo = request.getParameter("beAbleTo");
		String toOwn = request.getParameter("toOwn");
		String dificultyInCreditUnits = request.getParameter("dificultyInCreditUnits");
		String dificultyInHours = request.getParameter("dificultyInHours");
		String educationTecnology = request.getParameter("educationTecnology");
		String controlOfMaking = request.getParameter("controlOfMaking");
		String stuffOfDiscipline = request.getParameter("stuffOfDiscipline");
		Templates t = CreatorUtil.createTemplates(qualification,
				formOfStudy, author, statement, dateOfStatement, numberOfProtocol,
				toKnow, beAbleTo, toOwn, dificultyInCreditUnits, dificultyInHours,
				educationTecnology, controlOfMaking, stuffOfDiscipline);
		List<BaseEntity> toCreate = new ArrayList<BaseEntity>();
		toCreate.add(t);
		new Storage().storeAll(toCreate, null, null);
		// TODO: add ajax response
		// goToList(request, response);
	}

}